package com.zw.pm.module.config.client;

import com.zw.pm.base.BaseController;
import com.zw.pm.common.JsonResult;
import com.zw.pm.common.ValidResult;
import com.zw.pm.gen.entity.Client;
import com.zw.pm.gen.entity.Factory;
import com.zw.pm.module.config.factory.FactoryService;
import com.zw.pm.module.system.log.LogService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Description: 客户管理 控制层
 *
 * @Author: 徐一贺
 * @Date: 2020年3月21日12:56:28
 */
@Controller
@RequestMapping("/config/client")
public class ClientController extends BaseController {

    @Autowired
    private ClientService clientService;
    @Autowired
    private LogService logService;

    /**
     * 客户管理页面
     * @return 页面路径
     */
    @GetMapping
    @RequiresPermissions({"config:client"})
    public String page() {
        return "config/client/page";
    }

    /**
     * 系统客户数据
     * @return Json 数据
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"config:client"})
    public JsonResult list(Client client) {
        startPage();
        List<Client> list = clientService.findClientAll(client);
        return jsonResult(list);
    }
    /**
     * 系统客户页面数据
     * @return Json 数据
     */
    @ResponseBody
    @PostMapping("/allList")
    @RequiresPermissions(value={"config:client","project:build"},logical= Logical.OR)
    public JsonResult allList(Client client) {
        List<Client> list = clientService.findClientAll(client);
        return jsonResult(list);
    }

    /**
     * 名称唯一验证
     * @param client
     * @return json
     */
    @ResponseBody
    @PostMapping("/checkUnique")
    @RequiresPermissions({"config:client"})
    public ValidResult checkUnique(Client client) {
        Client clientObj = clientService.findClientByName(client.getName());
        if (client.getId() == null) {
            return ValidResult.result(clientObj);
        }
        if (clientObj == null || client.getId().equals(clientObj.getId())) {
            return ValidResult.SUCCESS;
        }
        return ValidResult.FAIL;
    }

    /**
     * 创建客户信息
     * @param client
     * @return json
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions({"config:client"})
    public JsonResult save(Client client) {
        logService.saveLog("创建客户信息","客户名:"+client.getName()+"分组排序："+client.getOrderNo());
        return jsonResult(clientService.saveClient(client));
    }

    /**
     * 根据ID查询
     * @param id id
     * @return Factory
     */
    @ResponseBody
    @PostMapping("/id")
    @RequiresPermissions({"config:client"})
    public Client id(Integer id) {
        return clientService.getClient(id);
    }

    /**
     * 修改客户信息
     * @param  client
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions({"config:client"})
    public JsonResult update(Client client) {
        Client before = clientService.getClient(client.getId());
        String operationContent="修改前客户信息：客户名:"+before.getName()+"分组排序："+before.getOrderNo();
        operationContent+="<font color='red'>修改后客户信息：客户名:"+client.getName()+"分组排序："+client.getOrderNo()+"</font>";
        logService.saveLog("修改客户信息",operationContent);
        return jsonResult(clientService.updateClient(client));
    }

    /**
     * 删除
     *
     * @param  client
     * @return json
     */
    @ResponseBody
    @PostMapping("/del")
    @RequiresPermissions({"config:client"})
    public JsonResult del(Client client) {
        Client before = clientService.getClient(client.getId());
        String operationContent="被删除客户户信息：客户名:"+before.getName()+"分组排序："+before.getOrderNo();
        logService.saveLog("删除客户信息",operationContent);
        return jsonResult(clientService.delClient(client));
    }

}
